home *** CD-ROM | disk | FTP | other *** search
- Path: anvil.ugrad.cs.ubc.ca!not-for-mail
- From: c2a192@ugrad.cs.ubc.ca (Kazimir Kylheku)
- Newsgroups: comp.lang.c
- Subject: Re: Simple Loop answer
- Date: 1 Mar 1996 15:07:27 -0800
- Organization: Computer Science, University of B.C., Vancouver, B.C., Canada
- Message-ID: <4h7vvfINNnj4@anvil.ugrad.cs.ubc.ca>
- References: <4h6dnf$fts@newsbf02.news.aol.com>
- NNTP-Posting-Host: anvil.ugrad.cs.ubc.ca
-
- In article <4h6dnf$fts@newsbf02.news.aol.com>, Tycope <tycope@aol.com> wrote:
- >Here is what I finally came up with.........
-
- You obviously did not read the replies.
-
-
- >#include <stdio.h>
- >#define MAX 1000
- >
- >long int i, j, k, l;
- >long int count = 0;
- >long int compares = 0;
- >
- >int
- >main (void)
- >{
- >
- >
- > for (i = 1; i <= MAX - 3; i++)
- > for (j = i + 1; j <= MAX - 2; j++)
- > for (k = j + 1; k <= MAX - 1; k++)
-
- Having a loop in 'l', and then computing the triplets for increasing 'l' is
- probably a more useful.
-
- > {
- > l = i + j + k;
- > compares++;
- >
- > if (((0 < i) && (i < j)) && ((j < k) && (k < l)))
-
- This is silly. What does the initialization for your i loop say? Can i _ever_
- be equal to, or less than zero?
-
- What does your initialization for the j loop say? Can it ever be lower than, or
- equal to i?
-
- Can k ever be <= j?
-
- Why do you bother checking conditions that are already satisfied by your loop
- controls?
-
- > { count++;
- > if (compares % 1000000 == 0)
-
- The original statement of the problem does not mention how the triplets are
- to be ordered. Thus taking every millionth one is meaningless, since such a
- sequence depends on the order in which the triplets are computed. You should
- document this.
- --
-
-